﻿#region File Description
//-----------------------------------------------------------------------------
// AnimatedTexture.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MiaProject
{
    public class AnimatedTexture
    {
        private int framecount;
        public Texture2D myTexture;
        private float TimePerFrame;
        public int Frame;
        private float TotalElapsed;
        private bool Paused;
        public int FrameWidth;
        public int FrameHeight;

        private int frame_i;
        private int frame_f;

        private int frame_i_change;
        private int frame_f_change;
        public bool startChange;
        public bool animEndend;


        public float Rotation, Scale, Depth;
        public Vector2 Origin;
        public AnimatedTexture(Vector2 origin, float rotation,
            float scale, float depth)
        {
            this.Origin = origin;
            this.Rotation = rotation;
            this.Scale = scale;
            this.Depth = depth;
            this.animEndend = false;
        }
        public void Load(ContentManager content, string asset,
            int frameCount, int framesPerSec)
        {
            framecount = frameCount;
            myTexture = content.Load<Texture2D>(asset);
            TimePerFrame = (float)1 / framesPerSec;
            Frame = 0;
            TotalElapsed = 0;
            Paused = false;
            FrameWidth = myTexture.Width / framecount;
            FrameHeight = myTexture.Height;

            this.Origin.X = FrameWidth / 2;
            this.Origin.Y = FrameHeight / 2;

            frame_i = 0;
            frame_f = frameCount;

            frame_i_change = 0;
            frame_i_change = 0;
            startChange = false;



        }
        public void setAnim(int i, int f)
        {
            frame_i = i;
            frame_f = f;
            Frame = i;
        }
        public void setAnimChanged(int i, int f)
        {
            frame_i_change = i;
            frame_f_change = f;
            startChange = true;
        }

        // class AnimatedTexture
        public void UpdateFrame(float elapsed)
        {

            if (Paused)
                return;
            if (Frame < frame_i)
                Frame = frame_i;
            TotalElapsed += elapsed;
            this.animEndend = false;

            if (TotalElapsed > TimePerFrame && Frame <= frame_f)
            {

                Frame++;
                // Keep the Frame between 0 and the total frames, minus one.
                checkChanged();
                //Frame = Frame % framecount;
                TotalElapsed -= TimePerFrame;
            }
            else
                checkChanged();

        }
        public void checkChanged()
        {
            if (Frame > frame_f)
            {
                if (startChange)//finalizou a animacao e tem que trocar
                {
                    startChange = false;
                    frame_i = frame_i_change;
                    frame_f = frame_f_change;
                }
                Frame = frame_i;
                this.animEndend = true;//fica como true um ciclo

            }
        }
        // class AnimatedTexture
        public void DrawFrame(SpriteBatch batch, Vector2 screenPos)
        {
            DrawFrame(batch, Frame, screenPos);
        }
        public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos)
        {
            Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0, FrameWidth, myTexture.Height);
            batch.Draw(myTexture, screenPos, sourcerect, Color.White, Rotation, Origin, Scale, SpriteEffects.None, Depth);
        }

        public bool IsPaused
        {
            get { return Paused; }
        }
        public void Reset()
        {
            Frame = 0;
            TotalElapsed = 0f;
        }
        public void Stop()
        {
            Pause();
            Reset();
        }
        public void Play()
        {
            Paused = false;
        }
        public void Pause()
        {
            Paused = true;
        }

    }
}
